<?php
namespace Home\Controller;

use Think\Controller;

/**
 * CommonController
 * 通用控制器
 */
class BaseController extends Controller {
    public function __construct(){
        parent::__construct();
		
		if(!isset($_SESSION['device_sn']) && trim($_GET['device_sn'])!=''){
			$_SESSION['device_sn'] = trim($_GET['device_sn']);
			$ifExists = M('user')->where(['device_sn'=>$_SESSION['device_sn']])->find();
			if(!$ifExists){
				$data = [];
				$data['device_sn'] = $_SESSION['device_sn'];
				$data['create_time'] = time();
				M('user')->add($data);
			}
		}
		if(!isset($_SESSION['device_sn'])){
			$this->ajaxReturn(['code'=>-1,'des'=>'无权访问']);
		}
        $this->uid = $_SESSION['device_sn'];  //用户唯一标识
    }

    //字符串处理成数组并过滤
    public function str_to_arr($str){
        return array_filter(explode(',',$str));
    }


    //判断用户是否阅读过某小说
    public function is_read_novel($novel_id){
        $device_sn = $this->uid;

        $is_read = M('readrecord')->where(['device_sn'=>$device_sn,'novel_id'=>$novel_id])->find();
        if($is_read){
            return 0;
        }else{
            return -1;
        }
    }


    //判断用户有没有上架某小说
    public function is_collect_novel($novel_id){
        $is_read = $this->is_read_novel($novel_id);
        if($is_read == 0){
            $read_record_Model = M('readrecord');
            $uid = $this->uid;

            $result = $read_record_Model->where(['novel_id'=>$novel_id,'device_sn'=>$uid])->getfield('is_bookrack');

            if($result == 1){
                return 0;
            }else{
                return -1;
            }
        }else{
            return -1;
        }
    }


    //获取小说类型中小说数量
    public function class_novel_number($class_id){
        $novel_Model = M('novel');
        $number = $novel_Model->where(['class_id'=>$class_id])->count();
        return $number;
    }

    //小说根据字段排序
    public function order_novel_list($where,$order_filed,$limit){
        $novel_Model = M('novel');
        $order_by[$order_filed] = 'desc';
        $where['is_del'] = 0;
        $where['is_pass'] = 1;
        $list = $novel_Model->order($order_by)->where($where)->limit($limit)->select();
        foreach($list as $key=>$val){
            if(!(strpos($val['cover'],'http://') === 0)){
                $list[$key]['cover'] = 'http://'.$_SERVER['HTTP_HOST'].$val['cover'];
            }
        }
        return $list;
    }

    //数字变中文
    public function number_to_word($number){
        if($number>999 && $number < 9999){
            return  $number/1000 .'千';
        }else if($number > 9999 && $number < 99999999){
            return  $number/10000 .'万';
        }else if($number > 99999999){
            return  $number/100000000 .'亿';
        }else{
            return $number;
        }
    }


    /**
     * 多维数组根据某键值大小排序
     * @param $array 要排序的数组
     * @param $keys 用来排序的键名
     * @param string $type 默认为升序排序
     * @return array
     */
    function array_sort($array,$keys,$type='asc'){
        $keysvalue = $new_array = array();
        foreach ($array as $k=>$v){
            $keysvalue[$k] = $v[$keys];
        }
        if($type == 'asc'){
            asort($keysvalue);
        }else{
            arsort($keysvalue);
        }

        reset($keysvalue);
        foreach ($keysvalue as $k=>$v){
            $new_array[$k] = $array[$k];
        }

        $show_arr = [];
        foreach($new_array as $key=>$val){
            $show_arr[] = $val;
        }
        return $show_arr;
    }

}
